<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_seleksi extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($dt){
		$this->db->flush_cache();
		$this->db->select('a.id_pendaftaran, a.PMBPeriodID, a.no_pendaftaran, a.nama, a.kota, a.prodi1, b.nama_prodi as nama_prodi1, a.prodi2, c.nama_prodi as nama_prodi2, a.alamat, a.no_telp, a.nama_ortu, a.alamat_ortu, a.pekerjaan');
		$this->db->from('pmb_pendaftaran a');
		$this->db->join('ref_prodi b', 'b.kd_prodi = a.prodi1');
		$this->db->join('ref_prodi c', 'c.kd_prodi = a.prodi2');
		$this->db->where('a.PMBPeriodID', $dt['PMBPeriodID']);
		$this->db->where('a.status', 'Registrasi');
		$this->db->limit($dt['jumlah'], 0);
		$this->db->order_by('a.no_pendaftaran', 'ASC');
		
		$q = $this->db->get()->result();
		
		$out = '';
		$i=1;
		$color = '';
		foreach($q as $r){
			$color = ($i % 2 == 0)?'#FFFFFF':'#fff9cf';
			$out .= '<tr>';
			$out .= '  <td bgcolor="'.$color.'">'.$i;
			$out .= '     <input type="hidden" name="data['.$i.'][id_pendaftaran]" value="'.$r->id_pendaftaran.'">';
			$out .= '     <input type="hidden" name="data['.$i.'][PMBPeriodID]" value="'.$r->PMBPeriodID.'">';
			$out .= '     <input type="hidden" name="data['.$i.'][no_pendaftaran]" value="'.$r->no_pendaftaran.'">';
			$out .= '     <input type="hidden" name="data['.$i.'][nama]" value="'.$r->nama.'">';
			$out .= '     <input type="hidden" name="data['.$i.'][kota]" value="'.$r->kota.'">';
			$out .= '     <input type="hidden" name="data['.$i.'][alamat]" value="'.$r->alamat.'">';
			$out .= '     <input type="hidden" name="data['.$i.'][no_telp]" value="'.$r->no_telp.'">';
			$out .= '     <input type="hidden" name="data['.$i.'][prodi1]" value="'.$r->prodi1.'">';
			$out .= '     <input type="hidden" name="data['.$i.'][prodi2]" value="'.$r->prodi2.'">';
			$out .= '     <input type="hidden" name="data['.$i.'][nama_ortu]" value="'.$r->nama_ortu.'">';
			$out .= '     <input type="hidden" name="data['.$i.'][alamat_ortu]" value="'.$r->alamat_ortu.'">';
			$out .= '     <input type="hidden" name="data['.$i.'][pekerjaan]" value="'.$r->pekerjaan.'">';
			$out .= '  </td>';
			$out .= '  <td bgcolor="'.$color.'">'.$r->no_pendaftaran.'</td>';
			$out .= '  <td bgcolor="'.$color.'">'.$r->nama.'</td>';
			$out .= '  <td bgcolor="'.$color.'">'.$r->nama_prodi1.'</td>';
			$out .= '  <td bgcolor="'.$color.'">'.$r->nama_prodi2.'</td>';
			$out .= '  <td bgcolor="'.$color.'">';
			$out .= '    <label><input type="radio" name="data['.$i.'][diterima]" value="pil1">Pil-1</label>&nbsp;&nbsp;&nbsp;';
			$out .= '    <label><input type="radio" name="data['.$i.'][diterima]" value="pil2">Pil-2</label>&nbsp;&nbsp;&nbsp;';
			$out .= '    <label><input type="radio" name="data['.$i.'][diterima]" value="pil3" checked="checked">Gagal</label>&nbsp;&nbsp;&nbsp;';
			$out .= '  </td>';
			$out .= '</tr>';
			$i++;
		}
		
		return $out;
	}
	
	function InsertOnDB($data){
		$this->db->trans_start();
		
		foreach($data as $row){
			if($row['diterima'] != 'pil3'){ // jika diterima
				# update table pendaftaran
				$this->db->flush_cache();
				$this->db->set('status', 'Lulus');
				$this->db->where('id_pendaftaran', $row['id_pendaftaran']);
				$this->db->update('pmb_pendaftaran');
				
				# insert to table mahasiswa
				$this->db->flush_cache();
				$this->db->set('PMBPeriodID', $row['PMBPeriodID']);
				$this->db->set('no_pendaftaran', $row['no_pendaftaran']);
				$this->db->set('nama', $row['nama']);
				$this->db->set('kota', $row['kota']);
				$this->db->set('alamat', $row['alamat']);
				$this->db->set('no_telp', $row['no_telp']);
				$this->db->set('kd_prodi', ($row['diterima']=='pil1'?$row['prodi1']:$row['prodi2']));
				$this->db->set('nama_ortu', $row['nama_ortu']);
				$this->db->set('alamat_ortu', $row['alamat_ortu']);
				$this->db->set('pekerjaan', $row['pekerjaan']);
				$this->db->set('status', 'Diterima');
				
				$this->db->set('CUID', $this->session->userdata('user_id'));
				$this->db->set('CDATE', date('Y-m-d H:i:s'));
				
				$this->db->insert('pmb_seleksi');
				
				# delete di tabel seleksi
				// $this->db->flush_cache();
				// $this->db->where('PMBPeriodID', $row['PMBPeriodID']);
				// $this->db->where('no_pendaftaran', $row['no_pendaftaran']);
				// $this->db->delete('pmb_pendaftaran');
				
			}else{ // jika gagal
				# update table pendaftaran
				$this->db->flush_cache();
				$this->db->set('status', 'Tidak Lulus');
				$this->db->where('id_pendaftaran', $row['id_pendaftaran']);
				$this->db->update('pmb_pendaftaran');
				
			}
		}
		
		$this->db->trans_complete();
	    return $this->db->trans_status();
	}
	
}

?>